import router from "./router";
import store from "./store";
import { Message } from "element-ui";
import NProgress from "nprogress"; // progress bar
import "nprogress/nprogress.css"; // progress bar style
NProgress.configure({ showSpinner: false }); // NProgress Configuration

const whiteList = "/trace-source-qrcode"; // no redirect whitelist

router.beforeEach(async (to, from, next) => {
  // console.log(store.state.permission._axiosPromiseArr)
  store.state.permission._axiosPromiseArr.forEach((ele, index) => {
    ele.cancel("跳转页面,结束请求") // 路由跳转之前，清空（终止）上一个页面正在请求的内容
  })
  store.commit('permission/clear_AxiosPromiseArr')
  // start progress bar
  NProgress.start();
  console.log("beforeEach", to);
  if (to.path.startsWith(whiteList)) {
    console.log("in white", to);
    next(to.path);
  } else {
    // other pages that do not have permission to access are redirected to the login page.
    // next(`/login?redirect=${to.path}`);
    next(`/404`);
    NProgress.done();
  }
});

router.afterEach((to) => {
  // finish progress bar
  NProgress.done();
  // console.log(to)
  // 多页控制 打开新的页面
  store.dispatch('page/open', to)
});
